package com.fzb.forum4j.util;


import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

import com.fzb.blog.po.Log;
import com.fzb.forum4j.po.Article;



public class XmlUtil {

	private static final XmlUtil instance =new XmlUtil();
	private XmlUtil(){};
	public static XmlUtil getInstance()
	{
		return instance;
	}
	@SuppressWarnings("deprecation")
	public boolean createSiteMap(List<Article> logs, HttpServletRequest req,String webhome) {
		Document doc=new Document();
		Element root=new Element("urlset");
		for (Log log : logs) {
			
			Element url=new Element("url");
			Element loc=new Element("loc");
			loc.setText(webhome+req.getContextPath()+"/post/"+log.getAlias());
			url.addContent(loc);
			Element lastmod=new Element("lastmod");
			lastmod.setText(DateParse.getInstance().toISO8601(log.getReleaseTime()));
			url.addContent(lastmod);
			Element changefreq=new Element("changefreq");
			changefreq.setText("daily");
			url.addContent(changefreq);
			Element priority=new Element("priority");
			priority.setText("1.0");
			url.addContent(priority);
			root.addContent(url);
		}
		doc.addContent(root);
		XMLOutputter out =new XMLOutputter(Format.getPrettyFormat());
		try {
			out.output(doc, new FileOutputStream(req.getRealPath("sitemap.xml")));
			return true;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
		
	}
	public static void main(String[] args) {
		//XmlUtil.getInstance().createSiteMap();
	}
	
	
}
